package com.korion.android.devicedemon

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.korion.android.devicedemon.base.LocalSettings
import com.korion.android.devicedemon.base.ThreadUtils
import com.korion.android.devicedemon.push.PushAPI
import java.text.SimpleDateFormat

class PushActivity : AppCompatActivity(), View.OnClickListener {

    private var accessToken: String = ""
    private var expired: Long  = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_push)
        LocalSettings.getString(LocalSettings.KEY_PUSH_APP_ID)?.run {
            findViewById<EditText>(R.id.edt_appId)?.setText(this)
        }
        LocalSettings.getString(LocalSettings.KEY_PUSH_APP_SECRET)?.run {
            findViewById<EditText>(R.id.edt_appSecret)?.setText(this)
        }
        LocalSettings.getString(LocalSettings.KEY_PUSH_OPEN_ID)?.run {
            findViewById<EditText>(R.id.edt_openId)?.setText(this)
        }
        LocalSettings.getString(LocalSettings.KEY_PUSH_ACCESS_TOKEN)?.run {
            findViewById<EditText>(R.id.edt_token)?.setText(this)
        }
        LocalSettings.getString(LocalSettings.KEY_ACCESS_TOKEN_EXPIRED)?.run {
            findViewById<TextView>(R.id.tv_tokenExpired)?.setText(toLocalTime(this.toLong()))
        }

        findViewById<EditText>(R.id.edt_message).setText(System.currentTimeMillis().toString())
    }

    override fun onClick(v: View) {
        when(v.id) {
            R.id.btn_getToken -> {
                val appId = findViewById<EditText>(R.id.edt_appId).text.toString().trim()
                val appSecret = findViewById<EditText>(R.id.edt_appSecret).text.toString().trim()
                if (appId.isNotEmpty()) {
                    LocalSettings.saveString(LocalSettings.KEY_PUSH_APP_ID, appId)
                }
                if (appSecret.isNotEmpty()) {
                    LocalSettings.saveString(LocalSettings.KEY_PUSH_APP_SECRET, appSecret)
                }
                if (appId.isEmpty() || appSecret.isEmpty()) {
                    return
                }
                ThreadUtils.execute {
                    val resp = PushAPI.getImpl().getAccessToken(appId, appSecret)
                    if (resp?.code == 0) {
                        accessToken = resp.accessToken
                        expired = System.currentTimeMillis() + resp.expire * 1000L
                        LocalSettings.saveString(LocalSettings.KEY_PUSH_ACCESS_TOKEN, accessToken)
                        LocalSettings.saveString(LocalSettings.KEY_ACCESS_TOKEN_EXPIRED, expired.toString())
                    }
                    if (resp != null) {
                        runOnUiThread {
                            findViewById<TextView>(R.id.tv_openId).text = resp.toString()
                            findViewById<EditText>(R.id.edt_token).setText(accessToken)
                            findViewById<TextView>(R.id.tv_tokenExpired).text = toLocalTime(expired)
                        }
                    }
                }
            }
            R.id.btn_push -> {
                val message = findViewById<EditText>(R.id.edt_message).text.toString().trim()
                val openId = findViewById<EditText>(R.id.edt_openId).text.toString().trim()
                val accessToken = findViewById<EditText>(R.id.edt_token).text.toString().trim()
                if (openId.isNotEmpty()) {
                    LocalSettings.saveString(LocalSettings.KEY_PUSH_OPEN_ID, openId)
                }
                if (openId.isEmpty()) {
                    return
                }
                ThreadUtils.execute {
                    val resp = PushAPI.getImpl().pushTextMessage(accessToken, openId, message)
                    if (resp != null) {
                        runOnUiThread {
                            findViewById<TextView>(R.id.tv_push_message).text = resp.toString()
                        }
                    }
                }
            }
        }
    }


    private fun toLocalTime(timestamp: Long): String {
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp)
    }
}